337C - Quiz - CodeForces Solution


binary search greedy math matrices number theory *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define fast_io ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

typedef long long ll;

ll mod(ll a, ll m) {                          // returns a (mod m)
  return ((a%m) + m) % m;                        // ensure positive answer
}

ll modPow(ll b, ll p, ll m) {                // assume 0 <= b < m
  if (p == 0) return 1;
  ll ans = modPow(b, p/2, m);                   // this is O(log p)
  ans = mod(ans*ans, m);                         // double it first
  if (p&1) ans = mod(ans*b, m);                  // *b if p is odd
  return ans;                                    // ans always in [0..m-1]
}

#define mo int(1000000009)

int main(){
    ll n,m,k;
    cin >> n >> m >> k;
    if(n >= 2*m){
        cout << m ;
    }else{
        ll sol = k;
        ll x = max(0ll, m - (n - n % k) / k * (k-1) - n % k);
        ll dble = modPow(2,x + 1,mo);
        sol = (sol * (dble-2) )%mo;
        sol += m - x*k;
        cout << mod(sol, mo);
    }
}


Comments

Submit
0 Comments
More Questions

1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64